<?php 
namespace app\admin\controller;

use think\Controller;
use think\facade\Validate;

/**
 * 广告管理
 */
class Advert extends Controller
{
	/*广告管理页面*/
	public function index()
	{
		if(request()->isAjax()){

			$map = [];
			$search = $this->request->param('search', '');
			$limit = $this->request->param('limit', 10);

			$data = model('Advert')
				->where($map)
				->order('weigh asc,create_time desc')
				->paginate($limit,false,['query'=>$this->request->param()]);

			$this->result(['data' => $data->items(), 'count' => $data->total()], 1, '获取成功', 'json');
		}

		return view();
	}

	/*添加广告*/
	public function add()
	{
		if(request()->isAjax()){
			$data = input();

			$rule = [
				'image' => 'require'
			];

			$msg = [
				'image.require' => '请上传图片'
			];

			$validate = Validate::make($rule,$msg);
			if(!$validate->check($data)){
				$this->result('',0,$validate->getError(),'json');
			}

			$data['is_show'] = isset($data['is_show']) ? 1 : 0;

			$res = model('Advert')->save($data);
			if($res){
				$this->result('',1,'添加成功','json');
			}else{
				$this->result('',0,'添加失败','json');
			}
		}

		return view();
	}

	/*编辑车型*/
	public function edit()
	{
		if(request()->isAjax()){
			$data = input();

			$rule = [
				'image' => 'require'
			];

			$msg = [
				'image.require' => '请上传图片'
			];

			$validate = Validate::make($rule,$msg);
			if(!$validate->check($data)){
				$this->result('',0,$validate->getError(),'json');
			}

			$data['is_show'] = isset($data['is_show']) ? 1 : 0;

			$res = model('Advert')->save($data,['id'=>$data['id']]);
			if($res){
				$this->result('',1,'编辑成功','json');
			}else{
				$this->result('',0,'编辑失败','json');
			}
		}

		$id = input('id');

		$data = model('Advert')::get($id);

		$this->assign('data',$data);
		
		return view();
	}

	/*删除*/
	public function delete()
	{
		$id = input('id');

		$res = model('Advert')->destroy($id);
		if($res){
			$this->result('',1,'删除成功','json');
		}else{
			$this->result('',0,'删除失败','json');
		}
	}

	//设置状态
	public function setStatus()
	{
		$data = input();

		$res = model('Advert')->save($data,['id'=>$data['id']]);
		if($res){
			$this->result('',1,'设置成功','json');
		}else{
			$this->result('',0,'设置失败','json');
		}
	}

}

 ?>